Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk-logs): ensure default resource attributes are used as fallbacks when a resource is passed to LoggerProvider #4564

Merged
merged 2 commits into from
Mar 21, 2024

Conversation

trentm
Copy link
Contributor

@trentm trentm commented Mar 20, 2024

Before this, Resource.default() attributes would only be used if no
resource was given to LoggerProvider. That would mean that
'service.name' and others could be missing, e.g. when called from
NodeSDK.


Here is where, for example, NodeSDK passes in a resource to LoggerProvider:

const loggerProvider = new LoggerProvider({
resource: this._resource,
});

…ks when a resource is passed to LoggerProvider

Before this Resource.default() attributes would only be used if *no*
resource was given to LoggerProvider. That would mean that
'service.name' and others could be missing, e.g. when called from
NodeSDK.
@trentm trentm self-assigned this Mar 20, 2024
@trentm trentm requested a review from a team March 20, 2024 23:39
@trentm
Copy link
Contributor Author

trentm commented Mar 20, 2024

small repro

Save this to "foo.js":

const { SeverityNumber } = require('@opentelemetry/api-logs');
const { LoggerProvider, SimpleLogRecordProcessor } = require('@opentelemetry/sdk-logs');
const { ExportResultCode } = require('@opentelemetry/core');
const { Resource } = require('@opentelemetry/resources');

class InspectLogRecordExporter {
  export(logs, resultCallback) {
    console.dir(logs, { depth: 5 });
    resultCallback({ code: ExportResultCode.SUCCESS });
  }
  shutdown() {
    return Promise.resolve();
  }
}

const loggerProvider = new LoggerProvider({
  resource: new Resource({ foo: 'bar' }),
});
loggerProvider.addLogRecordProcessor(
  new SimpleLogRecordProcessor(new InspectLogRecordExporter())
);

const logger = loggerProvider.getLogger('default');
logger.emit({
  severityNumber: SeverityNumber.INFO,
  severityText: 'INFO',
  body: 'this is a log record body',
  attributes: { anAttr: 'aValue' },
});

And run it without this fix:

% node foo.js
[
  LogRecord {
    attributes: { anAttr: 'aValue' },
    totalAttributesCount: 1,
    _isReadonly: false,
    hrTime: [ 1710977915, 946000000 ],
    hrTimeObserved: [ 1710977915, 946000000 ],
    _severityNumber: 9,
    _severityText: 'INFO',
    _body: 'this is a log record body',
    resource: Resource {
      _attributes: { foo: 'bar' },
      asyncAttributesPending: false,
      _syncAttributes: { foo: 'bar' },
      _asyncAttributesPromise: undefined
    },
    instrumentationScope: { name: 'default', version: undefined, schemaUrl: undefined },
    _logRecordLimits: { attributeCountLimit: 128, attributeValueLengthLimit: Infinity }
  }
]

That shows that the created LogRecord is missing the attributes from Resource.default(), e.g. service.name.
When run with the fix:

% node foo.js
[
  LogRecord {
    attributes: { anAttr: 'aValue' },
    totalAttributesCount: 1,
    _isReadonly: false,
    hrTime: [ 1710977947, 719000000 ],
    hrTimeObserved: [ 1710977947, 719000000 ],
    _severityNumber: 9,
    _severityText: 'INFO',
    _body: 'this is a log record body',
    resource: Resource {
      _attributes: {
        'service.name': 'unknown_service:node',
        'telemetry.sdk.language': 'nodejs',
        'telemetry.sdk.name': 'opentelemetry',
        'telemetry.sdk.version': '1.22.0',
        foo: 'bar'
      },
      asyncAttributesPending: false,
      _syncAttributes: {
        'service.name': 'unknown_service:node',
        'telemetry.sdk.language': 'nodejs',
        'telemetry.sdk.name': 'opentelemetry',
        'telemetry.sdk.version': '1.22.0',
        foo: 'bar'
      },
      _asyncAttributesPromise: undefined
    },
    instrumentationScope: { name: 'default', version: undefined, schemaUrl: undefined },
    _logRecordLimits: { attributeCountLimit: 128, attributeValueLengthLimit: Infinity }
  }
]

Copy link

codecov bot commented Mar 20, 2024

Codecov Report

Merging #4564 (aec0e9d) into main (aabd1a9) will increase coverage by 0.01%.
Report is 1 commits behind head on main.
The diff coverage is 100.00%.

❗ Current head aec0e9d differs from pull request most recent head fae06c4. Consider uploading reports for the commit fae06c4 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4564      +/-   ##
==========================================
+ Coverage   92.83%   92.84%   +0.01%     
==========================================
  Files         328      328              
  Lines        9486     9487       +1     
  Branches     2035     2035              
==========================================
+ Hits         8806     8808       +2     
+ Misses        680      679       -1     
Files Coverage Δ
...perimental/packages/sdk-logs/src/LoggerProvider.ts 97.82% <100.00%> (+0.04%) ⬆️

... and 1 file with indirect coverage changes

Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thanks 👍

@pichlermarc pichlermarc merged commit 6547440 into open-telemetry:main Mar 21, 2024
17 checks passed
@trentm trentm deleted the tm-sdk-logs-default-resource branch March 21, 2024 15:25
Zirak pushed a commit to Zirak/opentelemetry-js that referenced this pull request Sep 14, 2024
…ks when a resource is passed to LoggerProvider (open-telemetry#4564)

Before this Resource.default() attributes would only be used if *no*
resource was given to LoggerProvider. That would mean that
'service.name' and others could be missing, e.g. when called from
NodeSDK.

Co-authored-by: Marc Pichler <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants